package snapex.core.security;

import java.util.Map;

import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.SignatureVerifier;

public class JwtUtils {

	private final static JacksonJsonParser jacksonJsonParser = new JacksonJsonParser();
	
	public static Jwt decodeAndVerify(String token, SignatureVerifier verifier) {
		Jwt jwt = JwtHelper.decodeAndVerify(token, verifier);
		
		return jwt;
	}	
	
	public static boolean isExpired(Jwt jwt) {
		
		 Map<String, Object> claimMap = jacksonJsonParser.parseMap(jwt.getClaims());
		 
		 return System.currentTimeMillis() > ((Integer)claimMap.get("exp")) * 1000L;
	}
	
	public static boolean isExpired(String token) {
		
		 Map<String, Object> claimMap = jacksonJsonParser.parseMap(JwtHelper.decode(token).getClaims());
		 
		 return System.currentTimeMillis() > ((Integer)claimMap.get("exp")) * 1000L;
	}
}
